package com.huawei.gamebox.wallet.view;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.appmarket.service.export.CommonExportedActivity;
import com.huawei.hms.activity.BridgeActivity;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.api.HuaweiApiClient;
import com.huawei.hms.support.api.client.ResultCallback;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.entity.pay.InternalPayRequest;
import com.huawei.hms.support.api.pay.HuaweiPay;
import com.huawei.hms.support.api.pay.PayResult;
import com.huawei.hms.support.api.pay.PayResultInfo;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.lang.ref.WeakReference;
import o.cfi;
import o.dgh;
import o.dim;
import o.dmi;
import o.dmp;
import o.dmq;
import o.Logger;
import o.dye;
import o.eqf;
import o.eqj;
import o.eqr;
import o.fbj;
import o.fct;
import o.fcu;
import o.fcv;
import o.fcx;

public class WalletStartActivity extends CommonExportedActivity implements ResultCallback<PayResult> {
    private HuaweiApiClient ʻॱ;
    private fcu ˊॱ;
    protected String ˏॱ;
    private final int ͺ = 4005;
    private String ॱˊ;
    private dmi ॱˎ = new dmi(this) {
        final /* synthetic */ WalletStartActivity ˏ;

        {
            this.ˏ = r1;
        }

        public void run() {
            HuaweiPay.HuaweiPayApi.internalPay(this.ˏ.ʻॱ, this.ˏ.ᐝॱ()).setResultCallback(this.ˏ);
        }
    };

    static class a implements eqr {
        private WeakReference<WalletStartActivity> ˋ;

        a(WalletStartActivity walletStartActivity) {
            this.ˋ = new WeakReference(walletStartActivity);
        }

        private void ˏ() {
            WalletStartActivity walletStartActivity = this.ˋ == null ? null : (WalletStartActivity) this.ˋ.get();
            if (walletStartActivity != null) {
                walletStartActivity.finish();
            }
        }

        public void ॱ() {
            WalletStartActivity walletStartActivity = this.ˋ == null ? null : (WalletStartActivity) this.ˋ.get();
            if (walletStartActivity == null) {
                Logger.ˎ("WalletStartActivity", "onConnectSuccessed interrupt because of payActivity is null.");
            } else if (walletStartActivity.ॱˋ()) {
                walletStartActivity.ॱˎ();
            } else {
                Logger.ˎ("WalletStartActivity", "startPayClient interrupt beacause of checkHmsBranchValid false");
                ˏ();
            }
        }

        public void ॱ(int i) {
            Logger.ˋ("WalletStartActivity", "HwPayClient onConnectFailed，rtnCode:" + i);
            ˏ();
        }

        public void ˋ(int i) {
            if (Logger.ˎ()) {
                Logger.ˏ("WalletStartActivity", "HwPayClient onResolveError，support onResolveError:" + i);
            }
            if (this.ˋ == null) {
                Activity activity = null;
            } else {
                WalletStartActivity walletStartActivity = (WalletStartActivity) this.ˋ.get();
            }
            if (activity != null) {
                HuaweiApiAvailability.getInstance().resolveError(activity, i, 1000);
            } else {
                Logger.ˋ("WalletStartActivity", "HwPayClient onResolveError Interrupted,payActivity is null");
            }
        }
    }

    public /* synthetic */ void onResult(Object obj) {
        ˊ((PayResult) obj);
    }

    public void onCreate(Bundle bundle) {
        if (Logger.ˎ()) {
            Logger.ˏ("WalletStartActivity", "onCreate");
        }
        super.onCreate(bundle);
        if (getRequestedOrientation() == -1) {
            try {
                setRequestedOrientation(1);
            } catch (Throwable e) {
                Logger.ˋ("WalletStartActivity", " Exception.", e);
            }
        }
        if (bundle == null) {
            ʻॱ();
            dgh.ॱ(this, "15110206", ˊॱ());
            ॱॱ();
            return;
        }
        try {
            if (Logger.ˎ()) {
                Logger.ˏ("WalletStartActivity", "restore data");
            }
            this.ˊॱ = (fcu) bundle.getSerializable("payPrams");
            this.ˏॱ = bundle.getString("callerPid");
            this.ॱˊ = bundle.getString("currentFinishedCode");
        } catch (Exception e2) {
            Logger.e("WalletStartActivity", "get data from  data catch an exception");
            this.ॱˊ = "200003";
            finish();
        }
    }

    public cfi ᐝ() {
        cfi o_cfi = new cfi(this);
        o_cfi.ˏ(new dye(this));
        o_cfi.ˏ(new eqf(this));
        o_cfi.ˏ(new dim(this));
        o_cfi.ˏ(new fbj(this));
        return o_cfi;
    }

    public void ʼ() {
        if (this.ˊॱ == null) {
            this.ॱˊ = "200003";
            finish();
            return;
        }
        ॱˊ();
    }

    private String ˊॱ() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("01|");
        stringBuilder.append(UserSession.getInstance().getUserId()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
        stringBuilder.append(this.ˊॱ == null ? "" : this.ˊॱ.ʽ()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
        stringBuilder.append(this.ˊॱ == null ? "" : this.ˊॱ.ˋ()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
        stringBuilder.append(this.ˊॱ == null ? "" : this.ˊॱ.ॱॱ());
        return stringBuilder.toString();
    }

    protected void ॱˊ() {
        this.ʻॱ = eqj.ˋ(new a(this));
        if (ॱˋ()) {
            ॱˎ();
        }
    }

    private void ॱˎ() {
        dmq.ˏ.ˊ(dmp.ˋ, this.ॱˎ);
    }

    private void ʻॱ() {
        Bundle extras = new SafeIntent(getIntent()).getExtras();
        if (extras != null) {
            try {
                if (!extras.isEmpty()) {
                    Object string = extras.getString("pay");
                    this.ˏॱ = extras.getString("CallingPid");
                    if (TextUtils.isEmpty(string) || TextUtils.isEmpty(this.ˏॱ) || !"YES".equals(string)) {
                        Logger.e("WalletStartActivity", "init pay data error, pay is:" + string + " callerPid is:" + this.ˏॱ);
                        this.ॱˊ = "200003";
                        finish();
                        return;
                    }
                    this.ˊॱ = new fcu();
                    fct.ˎ(this.ˏॱ, this.ˊॱ);
                }
            } catch (Exception e) {
                Logger.e("WalletStartActivity", "init pay data catch an exception");
                this.ॱˊ = "200003";
                finish();
            }
        }
    }

    public void finish() {
        if (TextUtils.isEmpty(this.ॱˊ)) {
            this.ॱˊ = "200001";
        }
        fcv.ˋ(this.ˏॱ, this.ˊॱ, this, this.ॱˊ);
        super.finish();
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putSerializable("payPrams", this.ˊॱ);
        bundle.putString("callerPid", this.ˏॱ);
        bundle.putString("currentFinishedCode", this.ॱˊ);
    }

    private InternalPayRequest ᐝॱ() {
        InternalPayRequest internalPayRequest = new InternalPayRequest();
        internalPayRequest.productName = this.ˊॱ.ᐝ();
        internalPayRequest.productDesc = this.ˊॱ.ˊ();
        internalPayRequest.userID = this.ˊॱ.ˎ();
        internalPayRequest.applicationID = this.ˊॱ.ˋ();
        internalPayRequest.amount = this.ˊॱ.ˏ();
        internalPayRequest.requestId = this.ˊॱ.ॱॱ();
        internalPayRequest.sign = this.ˊॱ.ʻ();
        internalPayRequest.userName = this.ˊॱ.ॱ();
        internalPayRequest.sdkChannel = 3;
        internalPayRequest.serviceCatalog = "X6";
        internalPayRequest.extReserved = this.ˊॱ.ˋॱ();
        if (!TextUtils.isEmpty(this.ˊॱ.ˊॱ())) {
            internalPayRequest.signType = this.ˊॱ.ˊॱ();
        }
        internalPayRequest.notifyUrl = this.ˊॱ.ʼ();
        return internalPayRequest;
    }

    public void ˊ(PayResult payResult) {
        Status status = payResult.getStatus();
        if (status.isSuccess()) {
            try {
                status.startResolutionForResult(this, 4005);
                return;
            } catch (SendIntentException e) {
                Logger.e("WalletStartActivity", "startResolutionForResult Pay Failed:error msg");
                return;
            }
        }
        Logger.e("WalletStartActivity", "pay fail, status is:" + payResult.getStatus());
        this.ॱˊ = "200003";
        finish();
    }

    private boolean ॱˋ() {
        if (this.ʻॱ == null) {
            Logger.ˎ("WalletStartActivity", "startPayClient Failed，Rease：hwpayClient is null");
            finish();
            return false;
        } else if (this.ʻॱ.isConnected()) {
            return true;
        } else {
            Logger.ˎ("WalletStartActivity", "startPayClient Failed，Rease：hwpayClient not connected , wait hwpayClient connected ");
            return false;
        }
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        Logger.ˎ("WalletStartActivity", "Pay result onActivityResult :requestCode=" + i + ",resultCode=" + i2);
        boolean z = true;
        if (i == 4005) {
            ˊ(i2, intent);
        } else if (i == 1000) {
            z = ˎ(i2, intent);
        }
        if (z) {
            finish();
        }
    }

    private void ˊ(int i, Intent intent) {
        if (i == -1) {
            PayResultInfo payResultInfoFromIntent = HuaweiPay.HuaweiPayApi.getPayResultInfoFromIntent(intent);
            if (payResultInfoFromIntent != null) {
                int returnCode = payResultInfoFromIntent.getReturnCode();
                String ˋ = fcx.ˎ().ˋ(payResultInfoFromIntent, this.ˏॱ, this.ˊॱ);
                if (returnCode == 0) {
                    return;
                }
                if (returnCode == 30000) {
                    this.ॱˊ = "200001";
                    Logger.ˎ("WalletStartActivity", "user cancel hms pay");
                    return;
                }
                this.ॱˊ = ˋ;
                Logger.ˎ("WalletStartActivity", "hms return code:" + returnCode + "pay result is :" + ˋ);
                return;
            }
            this.ॱˊ = "200003";
            Logger.e("WalletStartActivity", "processPaySDKResult Failed, payResultInfo is null");
            return;
        }
        Logger.ˎ("WalletStartActivity", "Pay result onActivityResult Failed, canceld by user");
    }

    private boolean ˎ(int i, Intent intent) {
        if (i == -1) {
            int intExtra = intent.getIntExtra(BridgeActivity.EXTRA_RESULT, 0);
            if (intExtra == 0) {
                Logger.ˎ("WalletStartActivity", "Success!");
                ॱˊ();
                return false;
            } else if (intExtra == 13) {
                Logger.ˎ("WalletStartActivity", "Canceled!");
            } else if (intExtra == 8) {
                Logger.ˎ("WalletStartActivity", "Internal error!");
                this.ॱˊ = String.valueOf(intExtra);
            } else {
                this.ॱˊ = String.valueOf(intExtra);
                Logger.ˎ("WalletStartActivity", "Unknown!");
            }
        } else {
            Logger.ˎ("WalletStartActivity", "Error!");
        }
        return true;
    }
}
